package com.example.leepong.Activities;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.leepong.Activities.PlayActivity;
import com.example.leepong.DataHolder;
import com.example.leepong.Game.ChampionsMode.ChampionsMode;
import com.example.leepong.Game.Stage.AIPlayerConfiguration;
import com.example.leepong.R;
import com.example.leepong.Stage.PlayerProvider;

import java.util.ArrayList;


public class ChooseOpponentActivity extends Activity {
    private LinearLayout _playersContainer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.choose_opponent);

        TextView startText = (TextView) findViewById(R.id.startPlay);
        startText.setOnClickListener(new GoClickListener());

        _playersContainer = (LinearLayout) findViewById(R.id.co_playersContainer);
        fillPlayers(_playersContainer);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        unbindDrawables(findViewById(R.id.opponentRoot));
        System.gc();
    }

    private void unbindDrawables(View view) {
        if (view.getBackground() != null) {
            view.getBackground().setCallback(null);
        }
        if (view instanceof ViewGroup) {
            for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
                unbindDrawables(((ViewGroup) view).getChildAt(i));
            }
            ((ViewGroup) view).removeAllViews();
        }
    }

    private void fillPlayers(LinearLayout container) {
        // for some reason style does not keep layout params
        ViewGroup.LayoutParams savedLayout = container.getChildAt(0).getLayoutParams();
        container.removeAllViews();
        ArrayList<AIPlayerConfiguration> players = PlayerProvider.getInstance().getAvailableAiPlayers();

        for (AIPlayerConfiguration player : players) {
            ImageButton btn = createPlayerButton(player.logo);
            btn.setLayoutParams(savedLayout);
            btn.setOnClickListener(new PlayerClickListener());
            btn.setTag(player);
            container.addView(btn);
        }
    }

    private ImageButton createPlayerButton(int imageResource) {
        ImageButton button = (ImageButton) getLayoutInflater().inflate(R.layout.championbutton_template, null);
        button.setImageResource(imageResource);
        return button;
    }

    private View getSelectedPlayer() {
        for (int i = 0; i < _playersContainer.getChildCount(); i++) {
            View v = _playersContainer.getChildAt(i);
            if (v.isSelected()) {
                return v;
            }
        }
        return null;
    }

    private void selectPlayer(View view) {
        for (int i = 0; i < _playersContainer.getChildCount(); i++) {
            _playersContainer.getChildAt(i).setSelected(false);
        }
        view.setSelected(true);
    }

    private class PlayerClickListener implements View.OnClickListener {
        @Override
        public void onClick(View view) {
            selectPlayer(view);
        }
    }

    private class GoClickListener implements View.OnClickListener {
        @Override
        public void onClick(View view) {
            View selected = getSelectedPlayer();
            if (selected == null)
                return;

            AIPlayerConfiguration playerConfig = (AIPlayerConfiguration) selected.getTag();
            DataHolder.StageConfiguration = ChampionsMode.CreateStage(playerConfig);

            Intent playScreen = new Intent(getApplicationContext(), PlayActivity.class);
            startActivity(playScreen);
        }
    }
}
